package com.zcfeng.ischool;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import utils.MyConstaints;
import utils.ToastUtils;

public class RegistActivity extends AppCompatActivity {
    Button regist;
    Handler handler;
    EditText id;
    EditText pwd;
    EditText name;
    EditText academy;
    EditText professional;
    private MyApp app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        init_handler();
        app = (MyApp) this.getApplication();
        app.addHandle(MyConstaints.regist,handler);
        iniview();
        regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uid=id.getText().toString();
                String upwd=pwd.getText().toString();
                String uname=name.getText().toString();
                String uacademy=academy.getText().toString();
                String upro=professional.getText().toString();
                JSONObject json=new JSONObject();
                try {
                    json.put("type","regist");
                    json.put("id",uid);
                    json.put("pwd",upwd);
                    json.put("name",uname);
                    json.put("academy",uacademy);
                    json.put("professional",upro);
                    String str=json.toString();
                    System.out.println(str);
                    Message msg=new Message();
                    msg.what= MyConstaints.send;
                    msg.obj=str;
                    app.socket.revHandler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void iniview() {
        regist= (Button) findViewById(R.id.regist);
        id= (EditText) findViewById(R.id.id);
        pwd= (EditText) findViewById(R.id.pwd);
        name= (EditText) findViewById(R.id.name);
        academy= (EditText) findViewById(R.id.academy);
        professional= (EditText) findViewById(R.id.professional);

    }

    private void init_handler() {
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                String str=msg.obj.toString();
                try {
                    JSONObject obj=new JSONObject(str);
                    int code=obj.getInt("code");
                    if(code==200){
                        ToastUtils.safeToast(getApplicationContext(),"请等待审核。三日之内未通过则注册失败");
                        finish();
                    }
                    else{
                        ToastUtils.safeToast(getApplicationContext(),"注册失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
    }
    public void  back(View v){
        finish();

    }
}
